package com.symria.client.system;

import java.util.Date;
import java.util.List;

import com.symria.framework.page.ATTPage;
import com.symria.framework.page.Page;
import com.symria.framework.page.PagerFactory;
import com.symria.framework.util.BaseAction;
import com.symria.framework.util.FrameCache;
import com.symria.framework.util.MD5Util;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.system.ChannelService;
import com.symria.server.po.MenuInfo;
import com.symria.server.po.PackDateReport;
import com.symria.server.po.PackInfo;
import com.symria.server.po.SysUser;
import com.symria.server.po.UserInfo;
import com.symria.system.constants.ATTSystem;
import com.symria.system.constants.BaseAttribute;
import com.symria.system.util.SessionHelper;

public class ChannelAction extends BaseAction {

	private ChannelService channelService;
	private String userName;
	private String password;
	private String imgCode;
	private Date beginDate;
	private Date endDate;
	private PackDateReport pdr;
    private String rpassword;
    private String oldPassword;
	
	public Date getBeginDate() {
		return beginDate;
	}
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	public PackDateReport getPdr() {
		return pdr;
	}
	public void setPdr(PackDateReport pdr) {
		this.pdr = pdr;
	}
	public String getRpassword() {
		return rpassword;
	}
	public void setRpassword(String rpassword) {
		this.rpassword = rpassword;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getImgCode() {
		return imgCode;
	}
	public void setImgCode(String imgCode) {
		this.imgCode = imgCode;
	}
	public ChannelService getChannelService() {
		return channelService;
	}
	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 *功能描述:渠道登录
	 * 2013-9-6
	 * @return
	 * admin
	 */
	public String login(){
		UserInfo user = loginValidate();
		
		if(!hasFieldErrors()){
			channelService.updateUserForLogin(user);
			return SUCCESS;
		}
		return ERROR;
	}
	public UserInfo loginValidate(){
		UserInfo user = null;
		if(StringUtil.isEmpty(userName)){
			addFieldError("loginError", "请输入您的用户名!");
		}else if(StringUtil.isEmpty(password)){
			addFieldError("loginError", "请输入您的密码!");
		}else if(!StringUtil.equals(imgCode, SessionHelper.get(ATTSystem.SESSION_IMG_CODE, String.class))){
    		addFieldError("loginError", "您的验证码输入不正确!");
    	}else{
			user =  channelService.findUserByName(userName);
			if(null!=user){
				if(!StringUtil.equals(new MD5Util().getMD5ofStr(password), user.getPassword())){
					addFieldError("loginError", "您输入的密码错误!");
				}else if(!StringUtil.equals(user.getStatus(),ATTSystem.LOGIC_TRUE)){
					addFieldError("loginError", "您的账户未激活，请激活您的账户或联系管理员!");
				}
			}else{
				addFieldError("loginError", "渠道用户"+userName+"不存在,或者密码错误!");
			}
		}
		return user;
	}
	
	public String board(){
		UserInfo user = SessionHelper.getUserInfo();
		List<MenuInfo> list = FrameCache.getInstance().getMenuCacheByRole(user.getRoleId());
		setAttr(BaseAttribute.LIST, list);
		return SUCCESS;
	}
	public String searchPackReport(){
		UserInfo user = SessionHelper.getUserInfo();
		List<PackInfo> list = channelService.queryPackInfo(user.getUserId());
		if(pdr==null || StringUtil.isEmpty(pdr.getPackId())){
			pdr = new PackDateReport();
			pdr.setPackId(list.iterator().hasNext()?list.get(0).getId():0);
		}
		Page page = PagerFactory.createPage();
		channelService.searchPackReport(pdr, user, beginDate, endDate, page);
		setAttr(ATTPage.PAGE_LIST, page);
		setAttr(BaseAttribute.LIST, list);
		return SUCCESS;
	}
	public String updatePwd(){
		UserInfo u = SessionHelper.getUserInfo();
		if(!StringUtil.equals(password, rpassword)){
			addFieldError("error", "两次输入的密码不一致!");
		}else if(!StringUtil.equals(new MD5Util().getMD5ofStr(oldPassword), u.getPassword())){
			addFieldError("error", "原密码输入错误!");
		}else{
			u.setPassword(new MD5Util().getMD5ofStr(password));
			channelService.updateObject(u);
		}
		return hasFieldErrors()?ERROR:SUCCESS;
	}
	public String loginOut(){
		SessionHelper.invalidate();
		return SUCCESS;
	}
}
